package server

import (
	models "fileshare/controller/modles"
	"fileshare/sqlconn"
)

/**
* @program: fileshare
*
* @description:
*
* @author: Koco.Allien
*
* @create: 2020-04-06 18:30
**/

/*
to_userid=1 表示分享给大家 所有人
to_userid=自己 表示私有的
to_userid=别人的id 表示分想给别人的
*/
//获取私有文件列表//自己的文件,不含别人分享的
func GetSelfFiles(offset, limit, uid int) ([]models.BelongInfo, int) {
	var count int
	files := []models.BelongInfo{}
	sqlconn.MyDB.Model(&models.BelongInfo{}).Where("uid = ? && to_userid = ? ", uid, uid).Count(&count).
		Order("create_at desc").Offset(offset).Limit(limit).Find(&files)
	return files, count
}

//自己分享给别人
func GetToOtherFiles(offset, limit, uid int) ([]models.BelongInfo, int) {
	var count int
	files := []models.BelongInfo{}
	sqlconn.MyDB.Model(&models.BelongInfo{}).Where("uid = ?", uid).
		Not("to_userid in (?)", []int{0, 1, uid}).Count(&count).
		Order("create_at desc").Offset(offset).Limit(limit).Find(&files)
	return files, count
}

//别人分享给自己
func GetToMeFiles(offset, limit, uid int) ([]models.BelongInfo, int) {
	var count int
	files := []models.BelongInfo{}

	sqlconn.MyDB.Model(&models.BelongInfo{}).Where("to_userid = ?", uid).
		Not("uid = ? ", uid).Count(&count).
		Order("create_at desc").Offset(offset).Limit(limit).Find(&files)
	return files, count
}

//自己分享给大家
func GetToAllPeopleFiles(offset, limit, uid int) ([]models.BelongInfo, int) {
	var count int
	files := []models.BelongInfo{}
	sqlconn.MyDB.Model(&models.BelongInfo{}).Where("uid = ?", uid).
		Where("to_userid = ?", 1).Count(&count).
		Order("create_at desc").Offset(offset).Limit(limit).Find(&files)
	return files, count
}
